package com.lehecai.ucenter.action.server;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.lehecai.core.service.memcached.MemcachedService;
import com.lehecai.ucenter.action.BaseAction;
import com.lehecai.ucenter.entity.app.App;
import com.lehecai.ucenter.entity.permission.User;
import com.lehecai.ucenter.service.app.AppService;
import com.lehecai.ucenter.service.permission.UserService;
import com.lehecai.ucenter.utils.AuthorizeSignUtils;
import com.lehecai.ucenter.utils.AuthorizeTicketUtils;
import com.lehecai.ucenter.utils.MemcachedTicket;

public class ChangePwdAuthorizeAction extends BaseAction {
	
	private static final long serialVersionUID = -8634484692576627642L;
	
	private AppService appService;
	private MemcachedService memcachedService;
	private UserService userService;
	
	private User user;
	
	private String token;
	private String random;
	private String uid;
	private String sign;
	
	private String ticket;
	
	public String handle() {
		if (StringUtils.isEmpty(token) || StringUtils.isEmpty(random) || StringUtils.isEmpty(sign) || StringUtils.isEmpty(uid)) {
			logger.error("修改密码授权认证参数不全！");
			super.setErrorMessage("修改密码授权认证参数不全！");
			return "authorizeFailure";
		}
		App app = appService.findByToken(token);
		if (app == null) {
			logger.error("您所访问系统未经过用户中心授权！");
			super.setErrorMessage("您所访问系统未经过用户中心授权！");
			return "authorizeFailure";
		}
		Map<String, String> params = new HashMap<String, String>();
		params.put("token", token);
		params.put("random", random);
		params.put("uid", uid);
		if (!AuthorizeSignUtils.generateAuthorizeSign(params, app.getSecret()).equals(sign)) {
			logger.error("授权签名错误！");
			super.setErrorMessage("授权签名错误！");
			return "authorizeFailure";
		}
		user = userService.get(Long.valueOf(uid));
		if (user == null) {
			logger.error("未找到uid={}的用户", uid);
			super.setErrorMessage("未找到uid=" + uid + "的用户");
			return "failure";
		}
		ticket = AuthorizeTicketUtils.generateAuthorizeTicket();
		MemcachedTicket memcachedTicket = new MemcachedTicket();
		memcachedTicket.setTicket(ticket);
		memcachedTicket.setUid(uid);
		try {
			memcachedService.set(ticket, memcachedTicket, 60 * 5);
		} catch (Exception e) {
			logger.error("将ticket信息存入mc失败");
			logger.error(e.getMessage(), e);
		}
		logger.info("通过认证，返回修改密码页面");
		return "changePwd";
	}

	public AppService getAppService() {
		return appService;
	}

	public void setAppService(AppService appService) {
		this.appService = appService;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getRandom() {
		return random;
	}

	public void setRandom(String random) {
		this.random = random;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public String getTicket() {
		return ticket;
	}

	public void setTicket(String ticket) {
		this.ticket = ticket;
	}

	public MemcachedService getMemcachedService() {
		return memcachedService;
	}

	public void setMemcachedService(MemcachedService memcachedService) {
		this.memcachedService = memcachedService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
}
